package com.netty.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NIOClient {
    public static void main(String[] args) throws IOException {
        //得到一个网络通道
        SocketChannel socketChannel = SocketChannel.open();
        //设置非阻塞
        socketChannel.configureBlocking(false);
        //提供服务端的ip和端口
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1",6666);
        //连接服务器
        if (!socketChannel.connect(inetSocketAddress)){
            while (!socketChannel.finishConnect()){
                System.out.println("连接未成功 但是未阻塞");
            }
        }
        //连接成功 发送str
        String str = "hello netty";
        ByteBuffer buffer = ByteBuffer.wrap(str.getBytes());
        //发送数据 将buff数据写入channel
        socketChannel.write(buffer);
        System.in.read();
    }
}
